package com.eeepay.eeepay_v2.api;

import android.support.annotation.NonNull;

import com.eeepay.eeepay_v2.model.UserInfo;
import com.eeepay.eeepay_v2.util.Constant;
import com.eeepay.eeepay_v2.util.GrpcUtils;

import java.util.Arrays;
import java.util.List;
import java.util.concurrent.TimeUnit;

import cn.eeepay.api.grpc.nano.ManagementCenterProto;
import cn.eeepay.api.grpc.nano.SuperPushServiceGrpc;
import io.grpc.ManagedChannel;

/**
 * 描述：获取超级推分润查询记录GRPC
 * 作者：zhuangzeqin
 * 时间: 2017/8/10-10:24
 * 邮箱：zzq@eeepay.cn
 */
public class GetSuperPushShareRecordAPI {

    private static final int GETSUPERPUSHSHARERECORDCODE = 969;//请求的任务id

    private ResultCallBack resultCallBack;//设置监听接口

    private int mTotal;//总的记录数

    public final static int PAGESIZE = 10;//每页显示10条

    public GetSuperPushShareRecordAPI() {

    }

    /**
     * 开始获取超级推分润查询
     *
     * @param currPage 当前页码
     */
    public void getSuperPushShareRecordInfo(final int currPage) {
        if (resultCallBack == null) {
            throw new RuntimeException("ResultCallBack is null,you can must implement.");
        }
        if (currPage == 0) {
            throw new RuntimeException("currPage is not zero.");
        }
        resultCallBack.oninit();//请求前做一些初始化操作
        GrpcUtils.getInstance().doGrpcTask(Constant.URL.URL, Constant.URL.PORT, GETSUPERPUSHSHARERECORDCODE, new GrpcUtils.AsyncTaskListener() {
            @Override
            public Object doInBack(ManagedChannel channel, int taskId) {
                SuperPushServiceGrpc.SuperPushServiceBlockingStub stub =
                        SuperPushServiceGrpc.newBlockingStub(channel).withDeadlineAfter(30, TimeUnit.SECONDS);
                ManagementCenterProto.SuperPushShareRecordReq superPushShareRecordReq = new ManagementCenterProto.SuperPushShareRecordReq();
                superPushShareRecordReq.page= currPage;
                superPushShareRecordReq.pageSize = PAGESIZE;
                superPushShareRecordReq.userId = UserInfo.getUserInfo2SP().getAgentNo();
                return stub.getSuperPushShareRecord(superPushShareRecordReq);//获取超级推分润查询记录
            }

            @Override
            public void updateUI(Object result, int taskId) {
                resultCallBack.onUnInit();//返初始化操作
                if (result == null) {
                    resultCallBack.onFail("查询失败,请稍后重试!");
                    return;
                }
                ManagementCenterProto.SuperPushShareRecordRes superPushShareRecordRes = ( ManagementCenterProto.SuperPushShareRecordRes)result;
                if (!superPushShareRecordRes.resStatus.status) {
                    resultCallBack.onFail(superPushShareRecordRes.resStatus.msg);//返回错误信息
                    return;
                }
                mTotal = superPushShareRecordRes.total;
                if (mTotal==0)
                {
                    resultCallBack.onFail("暂时没有相关记录,请稍后重试!");
                    return;
                }
                ManagementCenterProto.SuperPushShareRecord[] superPushShareRecord = superPushShareRecordRes.superPushShareRecord;
                resultCallBack.onSucess(currPage,mTotal, Arrays.asList(superPushShareRecord));

            }
        });
    }

    /**
     * 设置监听回调
     *
     * @param resultCallBack
     */
    public void setResultCallBack(@NonNull ResultCallBack resultCallBack) {
        this.resultCallBack = resultCallBack;
    }

    /**
     * 提供外围接口；通过接口回传
     */
    public interface ResultCallBack {
        void oninit();//初始化

        void onFail(String msg);//失败返回；提示语

        void onSucess(int currPage, int total, List<ManagementCenterProto.SuperPushShareRecord> superPushShareRecords);//成功返回列表数据数据

        void onUnInit();//返初始化操作
    }
}
